package edzis.translatebuddy.view
{
	import edzis.translatebuddy.events.DataProxyEvent;
	import edzis.translatebuddy.model.AppProxy;
	import edzis.translatebuddy.model.DataProxy;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	import edzis.translatebuddy.ApplicationFacade;
	
	import edzis.translatebuddy.view.components.TranslateWord;

	public class TranslateWordMediator extends Mediator implements IMediator
	{
		
		public static const NAME:String = 'TranslateWordMediator';
		
		private var view			: TranslateWord;
		private var _dataProxy		: DataProxy;
		private var _appProxy		: AppProxy;
		
		
		public function TranslateWordMediator(viewComponent:Object = null) {
			super(NAME, viewComponent);
			view = viewComponent as TranslateWord;
			// Add Listeners
			view.addEventListener(DataProxyEvent.ADD_TRANSLATION, addTranslation);
		}
		
		private function addTranslation(e:DataProxyEvent):void {
			ArthropodDebug.log("addTranslation");
			_dataProxy.addTranslation(e.data.wordID, e.data.translation);
		}
		
		override public function onRegister():void {
			super.onRegister();
			_dataProxy = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
			_appProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
			view.init(_dataProxy.wordsSession, _dataProxy.allSearchProfiles);
		}
		
		
	/************************************
	/*	PRIVATE LISTENERS
	/***********************************/
		
		
		
		override public function listNotificationInterests():Array {
			return [
				AppProxy.APP_STATE_CHANGE,
				DataProxy.READY
			 ];
		}
		
		override public function handleNotification(notification:INotification):void {
			
			switch ( notification.getName() ) {
					
				case DataProxy.READY:
					_dataProxy.getAllSearchProfiles();
					break;
					
				case AppProxy.APP_STATE_CHANGE:
					if (notification.getBody() as String == AppProxy.ADD_TRANSLATION) view.visible = true;
					else view.visible = false;
					break;
					
				default : break;
			}
				
		}
		
	}
}